<?php
declare (strict_types=1);

namespace app\listener\pay;

use app\common\service\order\CouponOrderService;
use app\event\OrderPayRefund;
use app\event\PaySuccess;
use app\model\CouponOrder;
use app\model\Notify;
use app\model\PayLog;
use think\Exception;
use think\facade\Log;

class PaySuccessByPaylog
{
    /**
     * 事件监听处理
     * @param PaySuccess $event
     * @return mixed
     */
    public function handle($event)
    {
        $payLog = $event->obj;

        if (!($payLog instanceof PayLog)) {
            //无需处理非  pay log
            return true;
        }
        Log::info($payLog->couponOrder);
        if ($payLog->couponOrder->pay_status == CouponOrder::C_PAY_STATUS_YES) {
            throw new Exception('已支付，无需再支付1');
        }

        if ($payLog->status == PayLog::STATUS_PAY) {
            throw new Exception('已支付，无需再支付2');
        }

        $payLog->status = PayLog::STATUS_PAY;
        $payLog->save();

        $couponOrder = $payLog->couponOrder;
        $date = date('Y-m-d H:i:s');
        $couponOrder->pay_status = CouponOrder::C_PAY_STATUS_YES;
        $couponOrder->pay_amount = $payLog->real_pay_price;
        $couponOrder->pay_type = $payLog->pay_channel;
        $couponOrder->log .= $date . '支付成功';
        $couponOrder->pay_time = $date;
        $couponOrder->pay_log_id = $payLog->id;

        $couponOrder->save();


    }
}
